package com.example.server.service.implement;

import com.example.server.entity.Conversation;
import com.example.server.entity.Message;
import com.example.server.mapper.ConversationMapper;
import com.example.server.mapper.MessageMapper;
import com.example.server.service.ConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConversationServiceImpl implements ConversationService {

    @Autowired
    private ConversationMapper conversationMapper;

    @Autowired
    private MessageMapper messageMapper;

    // 创建Conversation
    @Override
    public Integer createConversation(Integer user_id,Integer script_id) {
//        conversationMapper.insertConversation(user_id);
        Conversation conversation = new Conversation();
        conversation.setUser_id(user_id);
        conversation.setScript_id(script_id);
        conversationMapper.insertConversation(conversation);
        return conversation.getId();
    }

    // 根据对话id拿到conversation
    @Override
    public Conversation getConversation(Integer conversation_id) {
        return conversationMapper.selectConversationById(conversation_id);
    }

    //根据用户id获取用户的历史对话
    @Override
    public List<Conversation> getUserConversations(Integer user_id) {
        return conversationMapper.selectConversationsByUserId(user_id);
    }

    // 保存用户和ai输入
    @Override
    public void saveMessage(Integer conversation_id, String sender, String content) {
        messageMapper.insertMessage(conversation_id,sender,content);
    }

    // 根据对话id获取消息记录
    @Override
    public List<Message> getMessages(Integer conversation_id) {
        return messageMapper.selectMessagesByConversationId(conversation_id);
    }

    @Override
    public void deleteConversation(Integer id) {
        // 先删除存档的对应 message
        messageMapper.deleteMessagesByConversationId(id);
        // 删除存档
        conversationMapper.deleteConversationById(id);
    }


}

